package com.manli.api.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.manli.api.dao.WeChatShareCutecatDao;
import com.manli.api.dto.WeChatShareCutecat;
import com.manli.api.pojo.*;
import com.manli.api.pojo.dingdanxia.*;
import com.manli.api.util.DingDanXiaUtils;
import com.manli.api.util.ObjectEmptyUtil;
import com.manli.api.util.ResultUtil;
import com.manli.api.util.XmlTool;
import com.manli.common.utils.HttpUtil;
import com.manli.common.utils.StringUtil;
import com.manli.taobao.TaoBaoKeUtil;
import com.manli.taobao.config.TaoBaoKeConfig;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.beetl.sql.core.SQLManager;
import org.beetl.sql.core.SQLReady;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.net.URLEncoder;
import java.util.*;

@Slf4j
@Service
public class TaoBaokeService {
    @Autowired
    private WeChatShareCutecatDao weChatShareCutecatDao;
    @Autowired
    private SQLManager sqlManager;
    @Autowired
    private DingDanXiaUtils dingDanXiaUtils;
    private static String dingdanxiaKey = "DcfeAj4wyZ875h32YYggeWr1kODgSbf1";
    private static String pddPid = "10786908_144780304";
    private static String jdUnionId = "1003016724";
    private static String jdPid = "1003016724_4000221715_3002569478";
    private static String cuteCatUrl = "http://127.0.0.1:8073/send";
//    private static String cuteCatUrl = "http://127.0.0.1:6060/post";



    public Result getImageByGoodsName(QueryParamsTaobaoke params) throws Exception{
        if(StringUtils.isEmpty(params.getGoodsName())){
            return ResultUtil.error(-1,"商品名参数缺失");
        }
//        List<Object> images = TaoBaoKeUtil.searchGoodsImgs(params.getGoodsName());

        JdToHelpersSuperQuery query = new JdToHelpersSuperQuery();
        query.setApikey(dingdanxiaKey);
        query.setQ(params.getGoodsName());
        JSONArray array = dingDanXiaUtils.taobaokeSuperSearch(query);
        List<String> urls = new ArrayList<>();
        for(int i=0;i<array.size();i++){
            JSONObject object = array.getJSONObject(i);
            if(object.get("small_images") != null){
                JSONObject smallImages = object.getJSONObject("small_images");
                if(smallImages.get("string") != null){
                    JSONArray imgs = smallImages.getJSONArray("string");
                    for(int j=0;j<imgs.size();j++){
                        urls.add(imgs.get(j).toString());
                    }
                }
            }
        }

        return ResultUtil.success(urls);
    }

    public Object cat(CuteCat cuteCat)throws Exception{
        if("100".equals(cuteCat.getType())){
            //私聊消息
            log.info("类型：私聊消息");
            CuteCatPrivateChat cuteCatPrivateChat = new CuteCatPrivateChat();
            cuteCatPrivateChat.setType("100");
            cuteCatPrivateChat.setTo_wxid(cuteCat.getFrom_wxid());
            cuteCatPrivateChat.setRobot_wxid(cuteCat.getRobot_wxid());

            if(cuteCat.getMsg().contains("推广号")){
                return bindExtensionId(cuteCat,cuteCatPrivateChat,cuteCat.getMsg().substring(cuteCat.getMsg().indexOf("号") + 1,cuteCat.getMsg().length()));
            }
            /**
             * 获取优惠券
             */
            return this.getCouponInfo(cuteCat,cuteCatPrivateChat);
        }else if("200".equals(cuteCat.getType())){
            //群聊消息
            log.info("类型：群聊消息");
            if(cuteCat.getMsg() == null){
                return null;
            }
            String msg = cuteCat.getMsg();

            if("提现".equals(msg)){
                return getCash(cuteCat);
            }

            if("历史提现".equals(msg)){
                return getCashHistory(cuteCat);
            }

            //如果是群里直接发分享xml
            if(cuteCat.getMsg().contains("<?xml")){
            //@机器人，反向@回去
                CuteCatPrublicChat cuteCatPrublicChat = new CuteCatPrublicChat();
                cuteCatPrublicChat.setType("102");
                cuteCatPrublicChat.setRobot_wxid(cuteCat.getRobot_wxid());
                cuteCatPrublicChat.setTo_wxid(cuteCat.getFrom_wxid());
                cuteCatPrublicChat.setAt_wxid(cuteCat.getFinal_from_wxid());
                cuteCatPrublicChat.setAt_name(cuteCat.getFinal_from_name());
                //先@原来的人
                Map map = new HashMap();
                map.put("data",JSONObject.toJSON(cuteCatPrublicChat).toString());
                HttpUtil.httpPost(cuteCatUrl,map);
                return this.getCouponInfoPublic(cuteCat,cuteCatPrublicChat);
            }

            if(msg!=null && msg.length()>=2 && msg.contains("[@")){
                if(msg.indexOf("[") != 0){
                    //@消息的格式：2333333[@at,nickname=Aha,wxid=wxid_rb57juwke6ek21]  我
                    String noSpace = msg.replace(" ","");
                    String msgReal = noSpace.substring(0,noSpace.indexOf("["));
                    List<String> stringList = Arrays.asList(noSpace.substring(noSpace.indexOf("["),noSpace.length() - 1).replace("[","").replace("]","").split(","));
                    //判断是否是@机器人，而不是其他用户
                    if(stringList.get(2).replace("wxid=","").equals(cuteCat.getRobot_wxid())) {
                        //确认是@机器人
                        //@机器人，反向@回去
                        CuteCatPrublicChat cuteCatPrublicChat = new CuteCatPrublicChat();
                        cuteCatPrublicChat.setType("102");
                        cuteCatPrublicChat.setRobot_wxid(cuteCat.getRobot_wxid());
                        cuteCatPrublicChat.setTo_wxid(cuteCat.getFrom_wxid());
                        cuteCatPrublicChat.setAt_wxid(cuteCat.getFinal_from_wxid());
                        cuteCatPrublicChat.setAt_name(cuteCat.getFinal_from_name());
                        //先@原来的人
                        Map map = new HashMap();
                        map.put("data",JSONObject.toJSON(cuteCatPrublicChat).toString());
                        HttpUtil.httpPost(cuteCatUrl,map);
                        cuteCat.setMsg(msgReal);
                        return this.getCouponInfoPublic(cuteCat,cuteCatPrublicChat);

                    }
                }else{
                    //@消息的格式：[@at,nickname=Aha,wxid=wxid_rb57juwke6ek21]  我
                    String[] atrArrs =  msg.split(" ");
                    List<String> stringList = Arrays.asList(atrArrs[0].replace("[","").replace("]","").split(","));
                    //判断是否是@机器人，而不是其他用户
                    if(stringList.get(2).replace("wxid=","").equals(cuteCat.getRobot_wxid())) {
                        //确认是@机器人
                        //@机器人，反向@回去
                        CuteCatPrublicChat cuteCatPrublicChat = new CuteCatPrublicChat();
                        cuteCatPrublicChat.setType("102");
                        cuteCatPrublicChat.setRobot_wxid(cuteCat.getRobot_wxid());
                        cuteCatPrublicChat.setTo_wxid(cuteCat.getFrom_wxid());
                        cuteCatPrublicChat.setAt_wxid(cuteCat.getFinal_from_wxid());
                        cuteCatPrublicChat.setAt_name(cuteCat.getFinal_from_name());
                        //先@原来的人
                        Map map = new HashMap();
                        map.put("data",JSONObject.toJSON(cuteCatPrublicChat).toString());
                        HttpUtil.httpPost(cuteCatUrl,map);
                        cuteCat.setMsg(atrArrs[2]);
                        return this.getCouponInfoPublic(cuteCat,cuteCatPrublicChat);

                    }
                }
            }else{
                //普通群聊消息，不做操作

            }
        }else if("500".equals(cuteCat.getType())){
            log.info("类型：好友申请");
//            JSONObject msg = JSONObject.parseObject(cuteCat.getMsg());
//            //分享名片的人
//            String sareWxid = msg.getString("share_wxid");
//            String shareNickname = msg.getString("share_nickname");
//            //添加者
//            String fromWxid = msg.getString("from_wxid");
//            String fromNickname = msg.getString("from_nickname");
//            //申请理由
//            String fromContent = msg.getString("from_content");
//            //机器人
//            String toWxid = msg.getString("to_wxid");
//            String toName = msg.getString("to_name");
//
//            WeChatShareCutecat weChatShareCutecat = new WeChatShareCutecat();
//            weChatShareCutecat.setFromWxid(fromWxid);
//            weChatShareCutecat.setFromNickname(fromNickname);
//            weChatShareCutecat.setShareWxid(sareWxid);
//            weChatShareCutecat.setShareNickname(shareNickname);
//            weChatShareCutecat.setToWxid(toWxid);
//            weChatShareCutecat.setToName(toName);
//            WeChatShareCutecat weChatShareCutecatSingle = weChatShareCutecatDao.createLambdaQuery().andEq("to_wxid",toWxid)
//                    .andEq("from_wxid",fromWxid).single();
//            if(weChatShareCutecatSingle == null){
//                weChatShareCutecatDao.insert(weChatShareCutecat);
//            }

//            //同意添加好友
//            Map<String,Object> map = new HashMap();
//            map.put("type",303);
//            map.put("robot_wxid",cuteCat.getRobot_wxid());
//            map.put("msg",msg);
//            Map data = new HashMap();
//            data.put("data",new JSONObject(map).toJSONString());
//            HttpUtil.httpPost(cuteCatUrl,data);


        }else if("400".equals(cuteCat.getType())){
            //群成员增加
            log.info("类型：群成员增加");
            JSONObject msg = JSONObject.parseObject(cuteCat.getMsg());
            String group_wxid = msg.getString("group_wxid");
            String group_name = msg.getString("group_name");
            JSONObject inviter = msg.getJSONObject("inviter");
            JSONArray guest = msg.getJSONArray("guest");
            List<String> guests = new ArrayList<>();
            for(int i=0;i<guest.size();i++) {
                JSONObject gu = (JSONObject) guest.get(i);
                WeChatShareCutecat weChatShareCutecat = new WeChatShareCutecat();
                weChatShareCutecat.setFromWxid(gu.getString("wxid"));
                weChatShareCutecat.setFromNickname(gu.getString("nickname"));
                weChatShareCutecat.setShareWxid(inviter.getString("wxid"));
                weChatShareCutecat.setShareNickname(inviter.getString("nickname"));
                weChatShareCutecat.setToWxid(group_wxid);
                weChatShareCutecat.setToName(group_name);
                weChatShareCutecat.setCreateTime(new Date());
                weChatShareCutecat.setStauts(0);
                WeChatShareCutecat weChatShareCutecatSingle = weChatShareCutecatDao.createLambdaQuery().andEq("to_wxid", group_wxid)
                        .andEq("from_wxid", gu.getString("wxid")).single();
                if (weChatShareCutecatSingle == null) {
                    weChatShareCutecatDao.insert(weChatShareCutecat);
                }

                //进群的人
                guests.add(gu.getString("nickname"));
                //先@进群的人
                CuteCatPrublicChat cuteCatPrublicChat = new CuteCatPrublicChat();
                cuteCatPrublicChat.setType("102");
                cuteCatPrublicChat.setRobot_wxid(cuteCat.getRobot_wxid());
                cuteCatPrublicChat.setTo_wxid(group_wxid);
                cuteCatPrublicChat.setAt_wxid(gu.getString("wxid"));
                cuteCatPrublicChat.setAt_name(gu.getString("nickname"));

                Map map = new HashMap();
                map.put("data", JSONObject.toJSON(cuteCatPrublicChat).toString());
                HttpUtil.httpPost(cuteCatUrl, map);

            }
            //本月拉人数量
            List<WeChatShareCutecat> list = weChatShareCutecatDao.execute("select * from we_chat_share_cutecat where DATE_FORMAT(create_time,'%Y-%m') = DATE_FORMAT(now(),'%Y-%m') and share_wxid = ?",inviter.getString("wxid"));

            //再@拉人的人
            CuteCatPrublicChat cuteCatPrublicChat = new CuteCatPrublicChat();
            cuteCatPrublicChat.setType("102");
            cuteCatPrublicChat.setRobot_wxid(cuteCat.getRobot_wxid());
            cuteCatPrublicChat.setTo_wxid(group_wxid);
            cuteCatPrublicChat.setAt_wxid(inviter.getString("wxid"));
            cuteCatPrublicChat.setAt_name(inviter.getString("nickname"));
            String msgRe = "恭喜" + inviter.getString("nickname") + "拉人成功！\n";
            msgRe += "本月已经拉了" + list.size() + "人了！\n";
            if(list.size() < 30){
                //拉人进群 拉10个人每人0.2元
                msgRe += "预计收入" + new BigDecimal(0.2*list.size()).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue() + "，加群主可提现哟~\n";
            }else{
                //30及以上人每个人0.5元
                msgRe += "预计收入" + new BigDecimal(0.5*list.size()).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue() + "，加群主可提现哟~\n";
            }

            msgRe += "----欢迎----\n";
            msgRe += JSONObject.toJSONString(guests);

            Map map = new HashMap();
            map.put("data", JSONObject.toJSON(cuteCatPrublicChat).toString());
            HttpUtil.httpPost(cuteCatUrl, map);
            cuteCatPrublicChat.setMsg(URLEncoder.encode(msgRe,"utf-8"));

            return cuteCatPrublicChat;
        }else if("910".equals(cuteCat.getType())){
            //账号下线
            log.info("类型：账号下线");
            return null;
        }
        return null;
    }

    /**
     * 获取优惠券信息，私聊
     * @param cuteCat
     * @param cuteCatPrivateChat
     * @return
     * @throws Exception
     */
    public Object getCouponInfo(CuteCat cuteCat,CuteCatPrivateChat cuteCatPrivateChat) throws Exception {
        if(cuteCat.getMsg().contains("https://item.taobao.com/item.htm") || cuteCat.getMsg().contains("https://m.tb.cn")){
            if(cuteCat.getMsg().contains("https://m.tb.cn")){
                //https://m.tb.cn短连接需要转长链接
                Map map = new HashMap();
                map.put("apikey",dingdanxiaKey);
                map.put("url",cuteCat.getMsg());
                String url = dingDanXiaUtils.tklShortToLongUrl(map);
                cuteCat.setMsg(url==null?cuteCat.getMsg():url);
            }

            //是淘宝的url，需要转义
            //查询淘宝优惠券
            TklToHelpersQuery tklToHelpersQuery = new TklToHelpersQuery();
            tklToHelpersQuery.setApikey(dingdanxiaKey);
            tklToHelpersQuery.setContent(URLEncoder.encode(cuteCat.getMsg(),"utf-8"));
            tklToHelpersQuery.setTpwd("true");
            tklToHelpersQuery.setItemInfo("true");
            //查询用户是否有推广渠道id
            String eid = getExtensionId(cuteCat.getFrom_wxid());
            if(eid != null){
                tklToHelpersQuery.setPid(TaoBaoKeConfig.SPECIAL_PID_TWO);
                tklToHelpersQuery.setRelation_id(eid);
            }

            //查询优惠券
            TklToHelpersReturn tklToHelpersReturn =  dingDanXiaUtils.tklToHelpers(tklToHelpersQuery);
            if(tklToHelpersReturn != null){
                cuteCatPrivateChat.setMsg(URLEncoder.encode(ReturnMsgTemplate.conpunMsg(tklToHelpersReturn),"utf-8"));
            }else{
                cuteCatPrivateChat.setMsg(URLEncoder.encode(ReturnMsgTemplate.noConpuns(),"utf-8"));
            }
        }else if(cuteCat.getMsg().contains("<?xml")){
            //拼多多或者京东的xml分享
            JSONObject xml =  XmlTool.documentToJSONObject(cuteCat.getMsg().replace("<?xml version=\"1.0\"?>",""));
            JSONObject appmsg = xml.getJSONArray("msg").getJSONObject(0);
            String appName = appmsg.getJSONArray("appinfo").getJSONObject(0).getString("appname");
            if("拼多多".equals(appName)){
                //拼多多的xml分享
                String url = appmsg.getJSONArray("appmsg").getJSONObject(0).getString("url");
                PddToHelpersQuery pddToHelpersQuery = new PddToHelpersQuery();
                pddToHelpersQuery.setApikey(dingdanxiaKey);
                pddToHelpersQuery.setWith_coupon("true");
                pddToHelpersQuery.setPid(pddPid);
                //获取到url中的goods_id
                pddToHelpersQuery.setGoods_id_list(StringUtil.urlGetParam(url,"goods_id"));
                //先查优惠券
                PddToHelpersReturn pddToHelpersReturn = dingDanXiaUtils.pddToHelpers(pddToHelpersQuery);
                //在转链（优惠券自动会有）
                PddToUrlHelpersQuery pddToUrlHelpersQuery = new PddToUrlHelpersQuery();
                pddToUrlHelpersQuery.setApikey(dingdanxiaKey);
                pddToUrlHelpersQuery.setGoods_id_list(StringUtil.urlGetParam(url,"goods_id"));
                pddToUrlHelpersQuery.setP_id(pddPid);
                pddToUrlHelpersQuery.setSearch_id(pddToHelpersReturn == null?null:pddToHelpersReturn.getSearchId());
                PddToUrlHelpersReturn pddToUrlHelpersReturn = dingDanXiaUtils.pddToUrlHelpers(pddToUrlHelpersQuery);

                if(pddToHelpersReturn != null && pddToUrlHelpersReturn != null){
                    Map<String,String> data = new HashMap<>();
                    data.put("title",ReturnMsgTemplate.shareTitle(pddToHelpersReturn.getCouponDiscount(),pddToHelpersReturn.getMinGroupPrice()));
                    data.put("text",pddToHelpersReturn.getGoodsDesc());
                    data.put("url",pddToUrlHelpersReturn.getShortUrl());
                    data.put("pic",pddToHelpersReturn.getGoodsImageUrl());
                    //返回可爱猫分享链接消息
                    CuteCatPrivateChat107 chat107 = new CuteCatPrivateChat107();
                    chat107.setType("107");
                    chat107.setTo_wxid(cuteCat.getFrom_wxid());
                    chat107.setRobot_wxid(cuteCat.getRobot_wxid());
                    chat107.setMsg(data);
                    return chat107;
                }else{
                    cuteCatPrivateChat.setMsg(URLEncoder.encode(ReturnMsgTemplate.noConpuns(),"utf-8"));
                }
            }else if("京东".equals(appName)){
                //京东的xml分享
                String url = appmsg.getJSONArray("appmsg").getJSONObject(0).getString("url");
                JdToHelpersQuery jdToHelpersQuery = new JdToHelpersQuery();
                jdToHelpersQuery.setApikey(dingdanxiaKey);
                jdToHelpersQuery.setIsCoupon("1");
                jdToHelpersQuery.setKeyword(url);
                jdToHelpersQuery.setPid(jdPid);
                //查询优惠券
                JdToHelpersReturn jdToHelpersReturn =  dingDanXiaUtils.jdToHelpers(jdToHelpersQuery);


                if(jdToHelpersReturn != null && jdToHelpersReturn != null){
                    Map<String,String> data = new HashMap<>();
                    List<JdcouponInfo> couponList = jdToHelpersReturn.getCouponInfo().getCouponList();
                    if(couponList != null && couponList.size() > 0){
                        data.put("title",ReturnMsgTemplate.shareTitleYuan(couponList.get(0).getDiscount(),jdToHelpersReturn.getPriceInfo().getLowestPrice(),jdToHelpersReturn.getCommissionInfo() == null?null:jdToHelpersReturn.getCommissionInfo().getCouponCommission()));
                        //在转链（优惠券自动会有）
                        JdToUrlHelpersQuery jdToUrlHelpersQuery = new JdToUrlHelpersQuery();
                        jdToUrlHelpersQuery.setApikey(dingdanxiaKey);
                        jdToUrlHelpersQuery.setMaterialId(url);
                        jdToUrlHelpersQuery.setSubUnionId(jdPid);
                        jdToUrlHelpersQuery.setUnionId(jdUnionId);
                        jdToUrlHelpersQuery.setCouponUrl(URLEncoder.encode(couponList.get(0).getLink(),"utf-8"));
                        JdToUrlHelpersReturn jdToUrlHelpersReturn = dingDanXiaUtils.jdToUrlHelpers(jdToUrlHelpersQuery);
                        if(jdToUrlHelpersReturn != null){
                            data.put("url",jdToUrlHelpersReturn.getShortUrl());
                        }else{
                            cuteCatPrivateChat.setMsg(URLEncoder.encode(ReturnMsgTemplate.noConpuns(),"utf-8"));
                            return cuteCatPrivateChat;
                        }
                    }else{
                        data.put("title",ReturnMsgTemplate.shareTitleYuan(null,jdToHelpersReturn.getPriceInfo().getLowestPrice(),jdToHelpersReturn.getCommissionInfo() == null?null:jdToHelpersReturn.getCommissionInfo().getCommission()));
                        //在转链,没有优惠券的转链
                        JdToUrlHelpersQuery jdToUrlHelpersQuery = new JdToUrlHelpersQuery();
                        jdToUrlHelpersQuery.setApikey(dingdanxiaKey);
                        jdToUrlHelpersQuery.setMaterialId(url);
                        jdToUrlHelpersQuery.setSubUnionId(jdPid);
                        jdToUrlHelpersQuery.setUnionId(jdUnionId);
                        JdToUrlHelpersReturn jdToUrlHelpersReturn = dingDanXiaUtils.jdToUrlHelpers(jdToUrlHelpersQuery);
                        if(jdToUrlHelpersReturn != null){
                            data.put("url",jdToUrlHelpersReturn.getShortUrl());
                        }else{
                            cuteCatPrivateChat.setMsg(URLEncoder.encode(ReturnMsgTemplate.noConpuns(),"utf-8"));
                            return cuteCatPrivateChat;
                        }
                    }

                    data.put("text",jdToHelpersReturn.getSkuName());

                    data.put("pic",jdToHelpersReturn.getImageInfo().getImageList().get(0).getUrl());
                    //返回可爱猫分享链接消息
                    CuteCatPrivateChat107 chat107 = new CuteCatPrivateChat107();
                    chat107.setType("107");
                    chat107.setTo_wxid(cuteCat.getFrom_wxid());
                    chat107.setRobot_wxid(cuteCat.getRobot_wxid());
                    chat107.setMsg(data);
                    return chat107;
                }else{
                    cuteCatPrivateChat.setMsg(URLEncoder.encode(ReturnMsgTemplate.noConpuns(),"utf-8"));
                }
            }

        }else if(cuteCat.getMsg().contains("yangkeduo.com")){
            //拼多多的url链接分享
            PddToHelpersQuery pddToHelpersQuery = new PddToHelpersQuery();
            pddToHelpersQuery.setApikey(dingdanxiaKey);
            pddToHelpersQuery.setWith_coupon("true");
            pddToHelpersQuery.setPid(pddPid);
            //获取到url中的goods_id
            pddToHelpersQuery.setGoods_id_list(StringUtil.urlGetParam(cuteCat.getMsg(),"goods_id"));
            //先查优惠券
            PddToHelpersReturn pddToHelpersReturn = dingDanXiaUtils.pddToHelpers(pddToHelpersQuery);
            //在转链（优惠券自动会有）
            PddToUrlHelpersQuery pddToUrlHelpersQuery = new PddToUrlHelpersQuery();
            pddToUrlHelpersQuery.setApikey(dingdanxiaKey);
            pddToUrlHelpersQuery.setGoods_id_list(StringUtil.urlGetParam(cuteCat.getMsg(),"goods_id"));
            pddToUrlHelpersQuery.setP_id(pddPid);
            pddToUrlHelpersQuery.setSearch_id(pddToHelpersReturn == null?null:pddToHelpersReturn.getSearchId());
            PddToUrlHelpersReturn pddToUrlHelpersReturn = dingDanXiaUtils.pddToUrlHelpers(pddToUrlHelpersQuery);

            if(pddToHelpersReturn != null && pddToUrlHelpersReturn != null){
                Map<String,String> data = new HashMap<>();
                data.put("title",ReturnMsgTemplate.shareTitle(pddToHelpersReturn.getCouponDiscount(),pddToHelpersReturn.getMinGroupPrice()));
                data.put("text",pddToHelpersReturn.getGoodsDesc());
                data.put("url",pddToUrlHelpersReturn.getShortUrl());
                data.put("pic",pddToHelpersReturn.getGoodsImageUrl());
                //返回可爱猫分享链接消息
                CuteCatPrivateChat107 chat107 = new CuteCatPrivateChat107();
                chat107.setType("107");
                chat107.setTo_wxid(cuteCat.getFrom_wxid());
                chat107.setRobot_wxid(cuteCat.getRobot_wxid());
                chat107.setMsg(data);
                return chat107;
            }else{
                cuteCatPrivateChat.setMsg(URLEncoder.encode(ReturnMsgTemplate.noConpuns(),"utf-8"));
            }


        }else if(cuteCat.getMsg().contains("jd.com")){
            //京东的url链接分享
            //查询京东优惠券
            JdToHelpersQuery jdToHelpersQuery = new JdToHelpersQuery();
            jdToHelpersQuery.setApikey(dingdanxiaKey);
            jdToHelpersQuery.setIsCoupon("1");
            jdToHelpersQuery.setKeyword(cuteCat.getMsg());
            jdToHelpersQuery.setPid(jdPid);
            //查询优惠券
            JdToHelpersReturn jdToHelpersReturn =  dingDanXiaUtils.jdToHelpers(jdToHelpersQuery);


            if(jdToHelpersReturn != null && jdToHelpersReturn != null){
                Map<String,String> data = new HashMap<>();
                List<JdcouponInfo> couponList = jdToHelpersReturn.getCouponInfo().getCouponList();
                if(couponList != null && couponList.size() > 0){
                    data.put("title",ReturnMsgTemplate.shareTitleYuan(couponList.get(0).getDiscount(),jdToHelpersReturn.getPriceInfo().getLowestPrice(),jdToHelpersReturn.getCommissionInfo() == null?null:jdToHelpersReturn.getCommissionInfo().getCouponCommission()));
                    //在转链（优惠券自动会有）
                    JdToUrlHelpersQuery jdToUrlHelpersQuery = new JdToUrlHelpersQuery();
                    jdToUrlHelpersQuery.setApikey(dingdanxiaKey);
                    jdToUrlHelpersQuery.setMaterialId(cuteCat.getMsg());
                    jdToUrlHelpersQuery.setSubUnionId(jdPid);
                    jdToUrlHelpersQuery.setUnionId(jdUnionId);
                    jdToUrlHelpersQuery.setCouponUrl(URLEncoder.encode(couponList.get(0).getLink(),"utf-8"));
                    JdToUrlHelpersReturn jdToUrlHelpersReturn = dingDanXiaUtils.jdToUrlHelpers(jdToUrlHelpersQuery);
                    if(jdToUrlHelpersReturn != null){
                        data.put("url",jdToUrlHelpersReturn.getShortUrl());
                    }else{
                        cuteCatPrivateChat.setMsg(URLEncoder.encode(ReturnMsgTemplate.noConpuns(),"utf-8"));
                        return cuteCatPrivateChat;
                    }
                }else{
                    data.put("title",ReturnMsgTemplate.shareTitleYuan(null,jdToHelpersReturn.getPriceInfo().getLowestPrice(),jdToHelpersReturn.getCommissionInfo() == null?null:jdToHelpersReturn.getCommissionInfo().getCommission()));
                    //在转链,没有优惠券的转链
                    JdToUrlHelpersQuery jdToUrlHelpersQuery = new JdToUrlHelpersQuery();
                    jdToUrlHelpersQuery.setApikey(dingdanxiaKey);
                    jdToUrlHelpersQuery.setMaterialId(cuteCat.getMsg());
                    jdToUrlHelpersQuery.setSubUnionId(jdPid);
                    jdToUrlHelpersQuery.setUnionId(jdUnionId);
                    JdToUrlHelpersReturn jdToUrlHelpersReturn = dingDanXiaUtils.jdToUrlHelpers(jdToUrlHelpersQuery);
                    if(jdToUrlHelpersReturn != null){
                        data.put("url",jdToUrlHelpersReturn.getShortUrl());
                    }else{
                        cuteCatPrivateChat.setMsg(URLEncoder.encode(ReturnMsgTemplate.noConpuns(),"utf-8"));
                        return cuteCatPrivateChat;
                    }
                }

                data.put("text",jdToHelpersReturn.getSkuName());

                data.put("pic",jdToHelpersReturn.getImageInfo().getImageList().get(0).getUrl());
                //返回可爱猫分享链接消息
                CuteCatPrivateChat107 chat107 = new CuteCatPrivateChat107();
                chat107.setType("107");
                chat107.setTo_wxid(cuteCat.getFrom_wxid());
                chat107.setRobot_wxid(cuteCat.getRobot_wxid());
                chat107.setMsg(data);
                return chat107;
            }else{
                cuteCatPrivateChat.setMsg(URLEncoder.encode(ReturnMsgTemplate.noConpuns(),"utf-8"));
            }


        }else{
            //是淘口令
            //查询淘宝优惠券
            TklToHelpersQuery tklToHelpersQuery = new TklToHelpersQuery();
            tklToHelpersQuery.setApikey(dingdanxiaKey);
            tklToHelpersQuery.setContent(cuteCat.getMsg());
            tklToHelpersQuery.setTpwd("true");
            tklToHelpersQuery.setItemInfo("true");
            //查询用户是否有推广渠道id
            String eid = getExtensionId(cuteCat.getFrom_wxid());
            if(eid != null){
                tklToHelpersQuery.setPid(TaoBaoKeConfig.SPECIAL_PID_TWO);
                tklToHelpersQuery.setRelation_id(eid);
            }

            //查询优惠券
            TklToHelpersReturn tklToHelpersReturn =  dingDanXiaUtils.tklToHelpers(tklToHelpersQuery);
            if(tklToHelpersReturn != null){
                cuteCatPrivateChat.setMsg(URLEncoder.encode(ReturnMsgTemplate.conpunMsg(tklToHelpersReturn),"utf-8"));
            }else{
                cuteCatPrivateChat.setMsg(URLEncoder.encode(ReturnMsgTemplate.noConpuns(),"utf-8"));
            }
        }

        log.info("类型：私聊消息，返回给机器人：" + cuteCatPrivateChat);
        return cuteCatPrivateChat;
    }

    /**
     * 获取优惠券信息，群聊
     * @return
     * @throws Exception
     */
    public Object getCouponInfoPublic(CuteCat cuteCat,CuteCatPrublicChat cuteCatPrivateChat) throws Exception {
        if(cuteCat.getMsg().contains("https://item.taobao.com/item.htm") || cuteCat.getMsg().contains("https://m.tb.cn")){
            if(cuteCat.getMsg().contains("https://m.tb.cn")){
                //https://m.tb.cn短连接需要转长链接
                Map map = new HashMap();
                map.put("apikey",dingdanxiaKey);
                map.put("url",cuteCat.getMsg());
                String url = dingDanXiaUtils.tklShortToLongUrl(map);
                cuteCat.setMsg(url==null?cuteCat.getMsg():url);
            }
            //是淘宝的url，需要转义
            //查询淘宝优惠券
            TklToHelpersQuery tklToHelpersQuery = new TklToHelpersQuery();
            tklToHelpersQuery.setApikey(dingdanxiaKey);
            tklToHelpersQuery.setContent(URLEncoder.encode(cuteCat.getMsg(),"utf-8"));
            tklToHelpersQuery.setTpwd("true");
            tklToHelpersQuery.setItemInfo("true");
            //查询用户是否有推广渠道id
            String eid = getExtensionId(cuteCat.getFrom_wxid());
            if(eid != null){
                tklToHelpersQuery.setPid(TaoBaoKeConfig.SPECIAL_PID_TWO);
                tklToHelpersQuery.setRelation_id(eid);
            }
            //查询优惠券
            TklToHelpersReturn tklToHelpersReturn =  dingDanXiaUtils.tklToHelpers(tklToHelpersQuery);
            if(tklToHelpersReturn != null){
                cuteCatPrivateChat.setMsg(URLEncoder.encode(ReturnMsgTemplate.conpunMsg(tklToHelpersReturn),"utf-8"));
            }else{
                cuteCatPrivateChat.setMsg(URLEncoder.encode(ReturnMsgTemplate.noConpuns(),"utf-8"));
            }
        }else if(cuteCat.getMsg().contains("<?xml")){
            //拼多多或者京东的xml分享
            JSONObject xml =  XmlTool.documentToJSONObject(cuteCat.getMsg().replace("<?xml version=\"1.0\"?>",""));
            JSONObject appmsg = xml.getJSONArray("msg").getJSONObject(0);
            String appName = appmsg.getJSONArray("appinfo").getJSONObject(0).getString("appname");
            if("拼多多".equals(appName)){
                //拼多多的xml分享
                String url = appmsg.getJSONArray("appmsg").getJSONObject(0).getString("url");
                PddToHelpersQuery pddToHelpersQuery = new PddToHelpersQuery();
                pddToHelpersQuery.setApikey(dingdanxiaKey);
                pddToHelpersQuery.setWith_coupon("true");
                pddToHelpersQuery.setPid(pddPid);
                //获取到url中的goods_id
                pddToHelpersQuery.setGoods_id_list(StringUtil.urlGetParam(url,"goods_id"));
                //先查优惠券
                PddToHelpersReturn pddToHelpersReturn = dingDanXiaUtils.pddToHelpers(pddToHelpersQuery);
                //在转链（优惠券自动会有）
                PddToUrlHelpersQuery pddToUrlHelpersQuery = new PddToUrlHelpersQuery();
                pddToUrlHelpersQuery.setApikey(dingdanxiaKey);
                pddToUrlHelpersQuery.setGoods_id_list(StringUtil.urlGetParam(url,"goods_id"));
                pddToUrlHelpersQuery.setP_id(pddPid);
                pddToUrlHelpersQuery.setSearch_id(pddToHelpersReturn == null?null:pddToHelpersReturn.getSearchId());
                PddToUrlHelpersReturn pddToUrlHelpersReturn = dingDanXiaUtils.pddToUrlHelpers(pddToUrlHelpersQuery);

                if(pddToHelpersReturn != null && pddToUrlHelpersReturn != null){
                    Map<String,String> data = new HashMap<>();
                    data.put("title",ReturnMsgTemplate.shareTitle(pddToHelpersReturn.getCouponDiscount(),pddToHelpersReturn.getMinGroupPrice()));
                    data.put("text",pddToHelpersReturn.getGoodsDesc());
                    data.put("url",pddToUrlHelpersReturn.getShortUrl());
                    data.put("pic",pddToHelpersReturn.getGoodsImageUrl());
                    //返回可爱猫分享链接消息
                    CuteCatPrivateChat107 chat107 = new CuteCatPrivateChat107();
                    chat107.setType("107");
                    chat107.setTo_wxid(cuteCat.getFrom_wxid());
                    chat107.setRobot_wxid(cuteCat.getRobot_wxid());
                    chat107.setMsg(data);
                    return chat107;
                }else{
                    cuteCatPrivateChat.setMsg(URLEncoder.encode(ReturnMsgTemplate.noConpuns(),"utf-8"));
                }
            }else if("京东".equals(appName)){
                //京东的xml分享
                String url = appmsg.getJSONArray("appmsg").getJSONObject(0).getString("url");
                JdToHelpersQuery jdToHelpersQuery = new JdToHelpersQuery();
                jdToHelpersQuery.setApikey(dingdanxiaKey);
                jdToHelpersQuery.setIsCoupon("1");
                jdToHelpersQuery.setKeyword(url);
                jdToHelpersQuery.setPid(jdPid);
                //查询优惠券
                JdToHelpersReturn jdToHelpersReturn =  dingDanXiaUtils.jdToHelpers(jdToHelpersQuery);


                if(jdToHelpersReturn != null){
                    Map<String,String> data = new HashMap<>();
                    List<JdcouponInfo> couponList = jdToHelpersReturn.getCouponInfo().getCouponList();
                    if(couponList != null && couponList.size() > 0){
                        data.put("title",ReturnMsgTemplate.shareTitleYuan(couponList.get(0).getDiscount(),jdToHelpersReturn.getPriceInfo().getLowestPrice(),jdToHelpersReturn.getCommissionInfo() == null?null:jdToHelpersReturn.getCommissionInfo().getCouponCommission()));
                        //在转链（优惠券自动会有）
                        JdToUrlHelpersQuery jdToUrlHelpersQuery = new JdToUrlHelpersQuery();
                        jdToUrlHelpersQuery.setApikey(dingdanxiaKey);
                        jdToUrlHelpersQuery.setMaterialId(url);
                        jdToUrlHelpersQuery.setSubUnionId(jdPid);
                        jdToUrlHelpersQuery.setUnionId(jdUnionId);
                        jdToUrlHelpersQuery.setCouponUrl(URLEncoder.encode(couponList.get(0).getLink(),"utf-8"));
                        JdToUrlHelpersReturn jdToUrlHelpersReturn = dingDanXiaUtils.jdToUrlHelpers(jdToUrlHelpersQuery);
                        if(jdToUrlHelpersReturn != null){
                            data.put("url",jdToUrlHelpersReturn.getShortUrl());
                        }else{
                            cuteCatPrivateChat.setMsg(URLEncoder.encode(ReturnMsgTemplate.noConpuns(),"utf-8"));
                            return cuteCatPrivateChat;
                        }
                    }else{
                        data.put("title",ReturnMsgTemplate.shareTitleYuan(null,jdToHelpersReturn.getPriceInfo().getLowestPrice(),jdToHelpersReturn.getCommissionInfo() == null?null:jdToHelpersReturn.getCommissionInfo().getCommission()));
                        //在转链,没有优惠券的转链
                        JdToUrlHelpersQuery jdToUrlHelpersQuery = new JdToUrlHelpersQuery();
                        jdToUrlHelpersQuery.setApikey(dingdanxiaKey);
                        jdToUrlHelpersQuery.setMaterialId(url);
                        jdToUrlHelpersQuery.setSubUnionId(jdPid);
                        jdToUrlHelpersQuery.setUnionId(jdUnionId);
                        JdToUrlHelpersReturn jdToUrlHelpersReturn = dingDanXiaUtils.jdToUrlHelpers(jdToUrlHelpersQuery);
                        if(jdToUrlHelpersReturn != null){
                            data.put("url",jdToUrlHelpersReturn.getShortUrl());
                        }else{
                            cuteCatPrivateChat.setMsg(URLEncoder.encode(ReturnMsgTemplate.noConpuns(),"utf-8"));
                            return cuteCatPrivateChat;
                        }
                    }

                    data.put("text",jdToHelpersReturn.getSkuName());

                    data.put("pic",jdToHelpersReturn.getImageInfo().getImageList().get(0).getUrl());
                    //返回可爱猫分享链接消息
                    CuteCatPrivateChat107 chat107 = new CuteCatPrivateChat107();
                    chat107.setType("107");
                    chat107.setTo_wxid(cuteCat.getFrom_wxid());
                    chat107.setRobot_wxid(cuteCat.getRobot_wxid());
                    chat107.setMsg(data);
                    return chat107;
                }else{
                    cuteCatPrivateChat.setMsg(URLEncoder.encode(ReturnMsgTemplate.noConpuns(),"utf-8"));
                }
            }

        }else if(cuteCat.getMsg().contains("yangkeduo.com")){
            //拼多多的url链接分享
            PddToHelpersQuery pddToHelpersQuery = new PddToHelpersQuery();
            pddToHelpersQuery.setApikey(dingdanxiaKey);
            pddToHelpersQuery.setWith_coupon("true");
            pddToHelpersQuery.setPid(pddPid);
            //获取到url中的goods_id
            pddToHelpersQuery.setGoods_id_list(StringUtil.urlGetParam(cuteCat.getMsg(),"goods_id"));
            //先查优惠券
            PddToHelpersReturn pddToHelpersReturn = dingDanXiaUtils.pddToHelpers(pddToHelpersQuery);
            //在转链（优惠券自动会有）
            PddToUrlHelpersQuery pddToUrlHelpersQuery = new PddToUrlHelpersQuery();
            pddToUrlHelpersQuery.setApikey(dingdanxiaKey);
            pddToUrlHelpersQuery.setGoods_id_list(StringUtil.urlGetParam(cuteCat.getMsg(),"goods_id"));
            pddToUrlHelpersQuery.setP_id(pddPid);
            pddToUrlHelpersQuery.setSearch_id(pddToHelpersReturn == null?null:pddToHelpersReturn.getSearchId());
            PddToUrlHelpersReturn pddToUrlHelpersReturn = dingDanXiaUtils.pddToUrlHelpers(pddToUrlHelpersQuery);

            if(pddToHelpersReturn != null && pddToUrlHelpersReturn != null){
                Map<String,String> data = new HashMap<>();
                data.put("title",ReturnMsgTemplate.shareTitle(pddToHelpersReturn.getCouponDiscount(),pddToHelpersReturn.getMinGroupPrice()));
                data.put("text",pddToHelpersReturn.getGoodsDesc());
                data.put("url",pddToUrlHelpersReturn.getShortUrl());
                data.put("pic",pddToHelpersReturn.getGoodsImageUrl());
                //返回可爱猫分享链接消息
                CuteCatPrivateChat107 chat107 = new CuteCatPrivateChat107();
                chat107.setType("107");
                chat107.setTo_wxid(cuteCat.getFrom_wxid());
                chat107.setRobot_wxid(cuteCat.getRobot_wxid());
                chat107.setMsg(data);
                return chat107;
            }else{
                cuteCatPrivateChat.setMsg(URLEncoder.encode(ReturnMsgTemplate.noConpuns(),"utf-8"));
            }


        }else if(cuteCat.getMsg().contains("jd.com")){
            //京东的url链接分享
            //查询京东优惠券
            JdToHelpersQuery jdToHelpersQuery = new JdToHelpersQuery();
            jdToHelpersQuery.setApikey(dingdanxiaKey);
            jdToHelpersQuery.setIsCoupon("1");
            jdToHelpersQuery.setKeyword(cuteCat.getMsg());
            jdToHelpersQuery.setPid(jdPid);
            //查询优惠券
            JdToHelpersReturn jdToHelpersReturn =  dingDanXiaUtils.jdToHelpers(jdToHelpersQuery);


            if(jdToHelpersReturn != null && jdToHelpersReturn != null){
                Map<String,String> data = new HashMap<>();
                List<JdcouponInfo> couponList = jdToHelpersReturn.getCouponInfo().getCouponList();
                if(couponList != null && couponList.size() > 0){
                    data.put("title",ReturnMsgTemplate.shareTitleYuan(couponList.get(0).getDiscount(),jdToHelpersReturn.getPriceInfo().getLowestPrice(),jdToHelpersReturn.getCommissionInfo() == null?null:jdToHelpersReturn.getCommissionInfo().getCouponCommission()));
                    //在转链（优惠券自动会有）
                    JdToUrlHelpersQuery jdToUrlHelpersQuery = new JdToUrlHelpersQuery();
                    jdToUrlHelpersQuery.setApikey(dingdanxiaKey);
                    jdToUrlHelpersQuery.setMaterialId(cuteCat.getMsg());
                    jdToUrlHelpersQuery.setSubUnionId(jdPid);
                    jdToUrlHelpersQuery.setUnionId(jdUnionId);
                    jdToUrlHelpersQuery.setCouponUrl(URLEncoder.encode(couponList.get(0).getLink(),"utf-8"));
                    JdToUrlHelpersReturn jdToUrlHelpersReturn = dingDanXiaUtils.jdToUrlHelpers(jdToUrlHelpersQuery);
                    if(jdToUrlHelpersReturn != null){
                        data.put("url",jdToUrlHelpersReturn.getShortUrl());
                    }else{
                        cuteCatPrivateChat.setMsg(URLEncoder.encode(ReturnMsgTemplate.noConpuns(),"utf-8"));
                        return cuteCatPrivateChat;
                    }
                }else{
                    data.put("title",ReturnMsgTemplate.shareTitleYuan(null,jdToHelpersReturn.getPriceInfo().getLowestPrice(),jdToHelpersReturn.getCommissionInfo() == null?null:jdToHelpersReturn.getCommissionInfo().getCommission()));
                    //在转链,没有优惠券的转链
                    JdToUrlHelpersQuery jdToUrlHelpersQuery = new JdToUrlHelpersQuery();
                    jdToUrlHelpersQuery.setApikey(dingdanxiaKey);
                    jdToUrlHelpersQuery.setMaterialId(cuteCat.getMsg());
                    jdToUrlHelpersQuery.setSubUnionId(jdPid);
                    jdToUrlHelpersQuery.setUnionId(jdUnionId);
                    JdToUrlHelpersReturn jdToUrlHelpersReturn = dingDanXiaUtils.jdToUrlHelpers(jdToUrlHelpersQuery);
                    if(jdToUrlHelpersReturn != null){
                        data.put("url",jdToUrlHelpersReturn.getShortUrl());
                    }else{
                        cuteCatPrivateChat.setMsg(URLEncoder.encode(ReturnMsgTemplate.noConpuns(),"utf-8"));
                        return cuteCatPrivateChat;
                    }
                }

                data.put("text",jdToHelpersReturn.getSkuName());

                data.put("pic",jdToHelpersReturn.getImageInfo().getImageList().get(0).getUrl());
                //返回可爱猫分享链接消息
                CuteCatPrivateChat107 chat107 = new CuteCatPrivateChat107();
                chat107.setType("107");
                chat107.setTo_wxid(cuteCat.getFrom_wxid());
                chat107.setRobot_wxid(cuteCat.getRobot_wxid());
                chat107.setMsg(data);
                return chat107;
            }else{
                cuteCatPrivateChat.setMsg(URLEncoder.encode(ReturnMsgTemplate.noConpuns(),"utf-8"));
            }


        }else{
            //是淘口令
            //查询淘宝优惠券
            TklToHelpersQuery tklToHelpersQuery = new TklToHelpersQuery();
            tklToHelpersQuery.setApikey(dingdanxiaKey);
            tklToHelpersQuery.setContent(cuteCat.getMsg());
            tklToHelpersQuery.setTpwd("true");
            tklToHelpersQuery.setItemInfo("true");
            //查询用户是否有推广渠道id
            String eid = getExtensionId(cuteCat.getFrom_wxid());
            if(eid != null){
                tklToHelpersQuery.setPid(TaoBaoKeConfig.SPECIAL_PID_TWO);
                tklToHelpersQuery.setRelation_id(eid);
            }
            //查询优惠券
            TklToHelpersReturn tklToHelpersReturn =  dingDanXiaUtils.tklToHelpers(tklToHelpersQuery);
            if(tklToHelpersReturn != null){
                cuteCatPrivateChat.setMsg(URLEncoder.encode(ReturnMsgTemplate.conpunMsg(tklToHelpersReturn),"utf-8"));
            }else{
                cuteCatPrivateChat.setMsg(URLEncoder.encode(ReturnMsgTemplate.noConpuns(),"utf-8"));
            }
        }

        log.info("类型：私聊消息，返回给机器人：" + cuteCatPrivateChat);
        return cuteCatPrivateChat;
    }

    public Result catStatic(QueryParamsCatStatic queryParamsCatStatic) {
        ObjectEmptyUtil.catStaticIsEmpty(queryParamsCatStatic);
        String sql = "SELECT " +
                " to_wxid, " +
                " to_name, " +
                " share_nickname, " +
                " share_wxid, " +
                " count(*) guests, " +
                "IF " +
                " ( count(*) < 30, count(*)* 0.2, count(*)* 0.5 ) money  " +
                "FROM " +
                " we_chat_share_cutecat where 1=1 " +
                isStart(queryParamsCatStatic.getStart()) +
                isend(queryParamsCatStatic.getEnd()) +
                isShare(queryParamsCatStatic.getShareName()) +
                "GROUP BY " +
                " to_wxid, " +
                " to_name, " +
                " share_nickname, " +
                " share_wxid";
        SQLReady sqlReady = new SQLReady( sql);
        PageQuery<Map> pageQuery = new PageQuery<Map>(queryParamsCatStatic.getPage().longValue(),queryParamsCatStatic.getLimit().longValue());
        PageQuery<Map> query = null;
        try {
            query = sqlManager.execute(sqlReady, Map.class,pageQuery);
        } catch (IndexOutOfBoundsException e) {
            return ResultUtil.success(new ArrayList(),0L);
        }
        return ResultUtil.success(query.getList(),query.getTotalRow());
    }

    public String isStart(String s){
        return StringUtils.isEmpty(s)?"":" and create_time >= '" + s + "'";
    }
    public String isend(String s){
        return StringUtils.isEmpty(s)?"":" and create_time <= '" + s + "'";
    }
    public String isShare(String s){
        return StringUtils.isEmpty(s)?"":" and share_nickname like '%"+ s + "%' ";
    }

    public Object getCash(CuteCat cuteCat)throws Exception{
        //本月拉人数量
        List<WeChatShareCutecat> list = weChatShareCutecatDao.execute("select * from we_chat_share_cutecat where DATE_FORMAT(create_time,'%Y-%m') = DATE_FORMAT(now(),'%Y-%m') and share_wxid = ?",cuteCat.getFinal_from_wxid());

        //再@拉人的人
        CuteCatPrublicChat cuteCatPrublicChat = new CuteCatPrublicChat();
        cuteCatPrublicChat.setType("102");
        cuteCatPrublicChat.setRobot_wxid(cuteCat.getRobot_wxid());
        cuteCatPrublicChat.setTo_wxid(cuteCat.getFrom_wxid());
        cuteCatPrublicChat.setAt_wxid(cuteCat.getFinal_from_wxid());
        cuteCatPrublicChat.setAt_name(cuteCat.getFinal_from_name());
        String msgRe = "恭喜" + cuteCat.getFinal_from_name() + "\n";
        msgRe += "本月已经拉了" + list.size() + "人了！\n";
        if(list.size() < 30){
            //拉人进群 拉10个人每人0.2元
            msgRe += "预计收入" + new BigDecimal(0.2*list.size()).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue() + "，加群主可提现哟~\n";
        }else{
            //30及以上人每个人0.5元
            msgRe += "预计收入" + new BigDecimal(0.5*list.size()).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue() + "，加群主可提现哟~\n";
        }

        Map map = new HashMap();
        map.put("data", JSONObject.toJSON(cuteCatPrublicChat).toString());
        HttpUtil.httpPost(cuteCatUrl, map);
        cuteCatPrublicChat.setMsg(URLEncoder.encode(msgRe,"utf-8"));

        return cuteCatPrublicChat;
    }

    public Object getCashHistory(CuteCat cuteCat)throws Exception{
        //本月拉人数量
        List<WeChatShareCutecat> list = weChatShareCutecatDao.execute("select * from we_chat_share_cutecat where DATE_FORMAT(create_time,'%Y-%m') = DATE_FORMAT(DATE_ADD(NOW(),INTERVAL -1 MONTH),'%Y-%m') and share_wxid = ?",cuteCat.getFinal_from_wxid());

        //再@拉人的人
        CuteCatPrublicChat cuteCatPrublicChat = new CuteCatPrublicChat();
        cuteCatPrublicChat.setType("102");
        cuteCatPrublicChat.setRobot_wxid(cuteCat.getRobot_wxid());
        cuteCatPrublicChat.setTo_wxid(cuteCat.getFrom_wxid());
        cuteCatPrublicChat.setAt_wxid(cuteCat.getFinal_from_wxid());
        cuteCatPrublicChat.setAt_name(cuteCat.getFinal_from_name());
        String msgRe = "恭喜" + cuteCat.getFinal_from_name() + "\n";
        msgRe += "上月已经拉了" + list.size() + "人了！\n";
        if(list.size() < 30){
            //拉人进群 拉10个人每人0.2元
            msgRe += "预计收入" + new BigDecimal(0.2*list.size()).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue() + "，加群主可提现哟~\n";
        }else{
            //30及以上人每个人0.5元
            msgRe += "预计收入" + new BigDecimal(0.5*list.size()).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue() + "，加群主可提现哟~\n";
        }

        Map map = new HashMap();
        map.put("data", JSONObject.toJSON(cuteCatPrublicChat).toString());
        HttpUtil.httpPost(cuteCatUrl, map);
        cuteCatPrublicChat.setMsg(URLEncoder.encode(msgRe,"utf-8"));

        return cuteCatPrublicChat;
    }

    public Object bindExtensionId(CuteCat cuteCat,CuteCatPrivateChat cuteCatPrublicChat,String extensionId)throws Exception{
        String sql = "select * from we_chat_extension_bind where wx_id = ?";
        SQLReady sqlReady = new SQLReady(sql,cuteCat.getFrom_wxid());
        List<Map> list = sqlManager.execute(sqlReady,Map.class);
        if(list.size() > 0){
            cuteCatPrublicChat.setMsg(URLEncoder.encode("您已经绑定过了","utf-8"));
        }else{
            String sql1 = "insert into we_chat_extension_bind(wx_id,extension_id) values(?,?)";
            SQLReady sqlReady1 = new SQLReady(sql1,cuteCat.getFrom_wxid(),extensionId.trim());
            sqlManager.executeUpdate(sqlReady1);
            cuteCatPrublicChat.setMsg(URLEncoder.encode("恭喜您，您已成功绑定推广id","utf-8"));
        }

        return cuteCatPrublicChat;
    }

    public String getExtensionId(String wxid)throws Exception{
        String sql = "select * from we_chat_extension_bind where wx_id = ?";
        SQLReady sqlReady = new SQLReady(sql,wxid);
        List<Map> list = sqlManager.execute(sqlReady,Map.class);
        if(list.size() > 0){
            return list.get(0).get("extensionId").toString();
        }else{
            return null;
        }
    }


}
